package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.DateUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ExperienceTimeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Service
@AttrHandler(ExperienceTimeAttrHandler.class)
public class ExperienceTimeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Resource
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteAttr experienceTimeAttr = inst.getAttr("200004102");
        if (Objects.nonNull(experienceTimeAttr)) {
            String experienceTime = experienceTimeAttr.getValue();
            Date effTime = new Date();
            // 起始时间
            KiteAttr startTimeAttr = inst.getAttr("200004293");
            if (Objects.nonNull(startTimeAttr)) {
                operateInstAttrService.setAttrValue(inst, "200004293", DateUtils.formatDate(effTime, DateUtils.DATE_FORMAT), data.getSceneInstId(), null);
            }
            // 三个月体验
            if ("100000159".equals(experienceTime)) {
                effTime = DateUtils.addMonth(effTime, 3);
            } else if ("100000160".equals(experienceTime)) {
                effTime = DateUtils.addMonth(effTime, 6);
            }
            if (KiteStringUtils.isNotEmpty(experienceTime)) {
                // 不需要计算时分秒，直接减一天
                effTime = DateUtils.addDay(effTime, -1);
            }
            // 结束时间
            KiteAttr endTimeAttr = inst.getAttr("200004294");
            if (Objects.nonNull(endTimeAttr)) {
                operateInstAttrService.setAttrValue(inst, "200004294", DateUtils.formatDate(effTime, DateUtils.DATE_FORMAT), data.getSceneInstId(), null);
            }
        }


    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
